package com.koala.learn.threadlearn.charter1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.DelayQueue;

/**
 * @Description TODO
 * @Date 2022/2/26 12:26
 * @Created by koala
 */

@Slf4j
public class Session {

    public static final int aliveTime = 3;
    DelayQueue<DelayElements> queue = new DelayQueue<>();
    private final ConcurrentHashMap<String, Object> userSession = new ConcurrentHashMap<>();

    /**
     * @Param
     */
    public void put(String key, Object o) {
        userSession.put(key, o);
        DelayElements delayElements = new DelayElements(key, aliveTime);
        queue.remove(key);
        queue.add(delayElements);
        log.info("添加Key :{}  存活时间为 ： {} ", key, aliveTime);
    }

    public void get(String key) {
        Object o = userSession.get(key);
        if (o != null) {
            DelayElements delayElements = new DelayElements(key, aliveTime);
            queue.remove(delayElements);
            queue.put(delayElements);
//            log.info();
        }
    }


}
